<?php
/**
 *
 * Author: Lyu
 * Date: 2023/7/25
 * Time: 15:53
 */

namespace App\Service\Core\Admin\Power;

use App\Model\Core\Admin\AdminUserModel;
use App\Model\Core\Admin\AdminUserPowerModel;
use App\Model\Core\Admin\AdminUserRoleModel;
use App\Service\Core\Admin\Menu\AdminMenuService;
use App\Service\Core\Admin\User\AdminUserService;
use App\Service\Core\CoreBaseService;
use App\Service\ParamsService;
use App\Traits\SingleInstanceTrait;

class AdminUserPowerService extends CoreBaseService
{
    use SingleInstanceTrait;

    public function getList($role_id, $is_cache = true): array
    {
        return AdminUserPowerModel::getInstance()->getList($role_id, $is_cache);
    }

    /**
     * 获取角色菜单权限
     * @param $user_id
     * @return array
     */
    public function getUserMenuList($user_id): array
    {
        if (empty($user_id)) {
            return alert_info(1, '获取用户角色权限失败');
        }
        $user_info = AdminUserService::getInstance()->getOne($user_id);
        if (empty($user_info)) {
            return alert_info(1, '用户不存在');
        }
        $menu = [];
        $button = [];
        //如果用户类型是业务员
        $role_ids = explode(',', $user_info['role_id']);
        if (in_array(1, $role_ids)) {
            $button = ['*/*/*'];
            $menu = AdminMenuService::getMenuTreeList();
        } else {
            $menu_list = [];
            foreach ($role_ids as $role_id) {
                $power_list = AdminUserPowerModel::getInstance()->getList($role_id);
                foreach ($power_list as $item) {
                    if ($item['link_type'] == ParamsService::ADMIN_USER_POWER_TYPE_MENU) {
                        $menu_info = AdminMenuService::getInstance()->getOne($item['link_id']);
                        if (!empty($menu_info)) {
                            $meta = json_decode($menu_info['meta'], true);
                            if ($meta['type'] == ParamsService::TYPE_MENU_BUTTON) {
                                $button[] = $menu_info['name'];
                            }
                            //获取所有父级
                            $parent_list = AdminMenuService::getInstance()->getParents($menu_info['parent_id']);
                            foreach ($parent_list as $v) {
                                $menu_list[$v['id']] = $v;
                            }
                            $menu_list[$menu_info['id']] = $menu_info;
                        }
                    }
                }
            }
            $menu = AdminMenuService::getInstance()->getMenuTreeByIds($menu_list);
        }
        return alert_info(0, 'ok', ['menu' => $menu, 'button' => $button]);
    }

    /**
     * 获取用户数据权限
     * @param $user_id
     * @return int
     */
    public function staffPermissions($user_id): int
    {
        $info = AdminUserModel::getInstance()->getOne((int)$user_id);
        $role_ids = explode(',', $info['role_id']);
        if (in_array(1, $role_ids)) {
            return ParamsService::ADMIN_USER_ROLE_PERMISSIONS_ALL;
        }
        $permissions = 0;
        foreach ($role_ids as $role_id) {
            $role_info = AdminUserRoleModel::getInstance()->getOne($role_id);
            if ($role_info['permissions'] > $permissions) {
                $permissions = $role_info['permissions'];
            }
        }
        return (int)$permissions;
    }

    public function getTypeList($role_id, $is_cache = true): array
    {
        $type_list = [];
        $list = AdminUserPowerModel::getInstance()->getList($role_id, $is_cache);
        foreach ($list as $item) {
            $type_list[$item['link_type']][] = $item['link_id'];
        }
        return $type_list;
    }
}
